package protocol.SZ_2021;

import java.util.HashMap;

public class Handle2021SZ8B implements IHandle2021SZ {
    @Override
    public String handle(SZ_2021Cmd cmd) {
        byte subOrder = cmd.subOrder();

        switch (subOrder){
            case 0x00:
                return handle8B00(cmd);
            case 0x01:
                return handle8B01(cmd);
            case 0x09:
                return handle8B09(cmd);
            case 0x10:
                return handle8B10(cmd);
            case 0x11:
                return handle8B11(cmd);
            case 0x0A:
                return handle8B0A(cmd);
            case 0x0B:
                return handle8B0B(cmd);
            default:
                return "深标:未匹配的命令字"+(cmd.readOrder()+","+cmd.readSubOrder());
        }
    }

    private String handle8B11(SZ_2021Cmd cmd) {
        StringBuilder builder = new StringBuilder();
        builder.append("平台->终端:中心巡检设备");
        builder.append("\r\n");
        while (cmd.leftSize()>0){
            builder.append("巡检设备类型代码:"+cmd.readInt());
            builder.append("\r\n");
        }

        return builder.toString();
    }

    private String handle8B0A(SZ_2021Cmd cmd) {
        return "平台->终端:中心确认报警";
    }

    private String handle8B0B(SZ_2021Cmd cmd) {
        return "平台->终端:中心解除报警";
    }

    private String handle8B10(SZ_2021Cmd cmd) {

        StringBuilder builder = new StringBuilder();
        builder.append("平台->终端:外围设备指令下行透传");
        builder.append("\r\n");

        int devType = cmd.readInt(1);
        int dataType = cmd.readInt(1);

        builder.append("目标外设:"+SZ_2021Utils.toDeviceType(devType));
        builder.append("\r\n");

        HashMap<Integer,String> list = new HashMap<>();
        list.put(0,"数据无压缩");
        list.put(1,"gz压缩");

        builder.append("数据压缩算法:"+list.getOrDefault(dataType&0x07,"未定义"));
        builder.append("\r\n");

        builder.append("数据格式:"+(((dataType>>3)&0x01) == 1?"密文":"明文"));
        builder.append("\r\n");

        builder.append("数据包:"+cmd.readBCD(cmd.leftSize()));
        builder.append("\r\n");

        return builder.toString();
    }

    private String handle8B09(SZ_2021Cmd cmd) {
        StringBuilder builder = new StringBuilder();
        builder.append("平台->终端:中心取消订单");
        builder.append("\r\n");

        builder.append("业务ID:"+cmd.readInt(4));
        builder.append("\r\n");

        return builder.toString();
    }

    private String handle8B01(SZ_2021Cmd cmd) {
        StringBuilder builder = new StringBuilder();
        builder.append("平台->终端:下发抢答结果信息");
        builder.append("\r\n");

        builder.append("业务ID:"+cmd.readInt(4));
        builder.append("\r\n");

        HashMap<Integer,String> list = new HashMap<>();
        list.put(0,"即时召车");
        list.put(1,"预约召车");
        list.put(2,"车辆指派");
        builder.append("业务类型:"+list.getOrDefault(cmd.readInt(1),"未定义"));
        builder.append("\r\n");

        builder.append("用车时间:"+cmd.readBCD(6));
        builder.append("\r\n");

        builder.append("乘客位置经度:"+cmd.readInt(4));
        builder.append("\r\n");

        builder.append("乘客位置纬度:"+cmd.readInt(4));
        builder.append("\r\n");

        builder.append("目的地位置经度:"+cmd.readInt(4));
        builder.append("\r\n");

        builder.append("目的地位置纬度:"+cmd.readInt(4));
        builder.append("\r\n");

        builder.append("电召服务费:"+cmd.readBCD(2));
        builder.append("\r\n");

        builder.append("乘客电话号码:"+cmd.readString(-1));
        builder.append("\r\n");

        builder.append("业务描述:"+cmd.readString(cmd.leftSize()));
        builder.append("\r\n");

        return builder.toString();
    }


    private String handle8B00(SZ_2021Cmd cmd) {
        StringBuilder builder = new StringBuilder();
        builder.append("平台->终端:订单任务下发");
        builder.append("\r\n");

        builder.append("业务ID:"+cmd.readInt(4));
        builder.append("\r\n");

        HashMap<Integer,String> list = new HashMap<>();
        list.put(0,"即时召车");
        list.put(1,"预约召车");
        list.put(2,"车辆指派");
        builder.append("业务类型:"+list.getOrDefault(cmd.readInt(1),"未定义"));
        builder.append("\r\n");

        builder.append("要车时间:"+cmd.readBCD(6));
        builder.append("\r\n");

        builder.append("乘客下单位置经度:"+cmd.readInt(4));
        builder.append("\r\n");

        builder.append("乘客下单位置纬度:"+cmd.readInt(4));
        builder.append("\r\n");

        builder.append("业务描述:"+cmd.readString(cmd.leftSize()));
        builder.append("\r\n");

        builder.append("电召服务费:"+cmd.readBCD(2));
        builder.append("\r\n");

        return builder.toString();
    }

}
